#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;
ll a[N], l[N], r[N];
int stk[N], top;

void solve() {
	int n; cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		while (top && a[stk[top]] >= a[i]) top--;
		if (!top) l[i] = 0;
		else l[i] = stk[top];
		stk[++top] = i;
	}
	top = 0;
	for (int i = n; i >= 1; i--) {
		while (top && a[stk[top]] > a[i]) top--;
		if (!top) r[i] = n + 1;
		else r[i] = stk[top];
		stk[++top] = i;
	}
	ll ans = 0;
	for (int i = 1; i <= n; i++) ans += a[i] * (i - l[i]) * (r[i] - i);
	cout << ans << '\n';
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	solve();
	return 0;
}